<template>
  <el-dialog
    :title="account ? '编辑账号' : '添加账号'"
    v-model="dialogVisible"
    width="500px"
    @close="handleClose"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="100px"
      @submit.prevent="handleSubmit"
    >
      <el-form-item label="系统名称" prop="system_name">
        <el-input v-model="form.system_name" placeholder="请输入系统名称" />
      </el-form-item>
      <el-form-item label="账户名称" prop="username">
        <el-input v-model="form.username" placeholder="请输入账户名称" />
      </el-form-item>
      <el-form-item :label="account ? '新密码' : '账户密码'" prop="password">
        <el-input
          v-model="form.password"
          type="password"
          placeholder="请输入密码"
          show-password
        />
        <div class="form-tip" v-if="account">留空表示不修改密码</div>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="form.remark"
          type="textarea"
          :rows="3"
          placeholder="请输入备注信息"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="loading">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'

const props = defineProps({
  visible: Boolean,
  account: Object
})

const emit = defineEmits(['update:visible', 'submit'])

const formRef = ref(null)
const loading = ref(false)
const form = ref({
  system_name: '',
  username: '',
  password: '',
  remark: ''
})

const rules = {
  system_name: [{ required: true, message: '请输入系统名称', trigger: 'blur' }],
  username: [{ required: true, message: '请输入账户名称', trigger: 'blur' }],
  password: [{ required: !props.account, message: '请输入密码', trigger: 'blur' }]
}

const dialogVisible = computed({
  get: () => props.visible,
  set: (val) => emit('update:visible', val)
})

watch(
  () => props.account,
  (account) => {
    if (account) {
      form.value = {
        id: account.id,
        system_name: account.system_name,
        username: account.username,
        password: '', // 清空密码
        remark: account.remark || ''
      }
    } else {
      form.value = {
        system_name: '',
        username: '',
        password: '',
        remark: ''
      }
    }
  },
  { immediate: true }
)

const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    loading.value = true
    
    // 构造提交的数据
    const submitData = {
      system_name: form.value.system_name,
      username: form.value.username,
      password: form.value.password || '',
      remark: form.value.remark || ''
    }
    
    // 如果是编辑模式，添加id
    if (props.account) {
      submitData.id = props.account.id
    }

    // 打印请求数据（调试用）
    console.log('Submitting account data:', submitData)
    
    await emit('submit', submitData)
  } catch (error) {
    if (error.message) {
      ElMessage.error(error.message)
    }
  } finally {
    loading.value = false
  }
}

const handleClose = () => {
  formRef.value?.resetFields()
}
</script>

<style scoped>
.form-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 4px;
}
</style> 